


//=============================================================================
// Graphics
//=============================================================================

if (Yanfly.Param.UpdateRealScale) {

    Graphics._updateRealScale = function() {
      if (this._stretchEnabled) {
        var h = window.innerWidth / this._width;
        var v = window.innerHeight / this._height;
        this._realScale = Math.min(h, v);
        if (this._realScale >= 3) this._realScale = 3;
        else if (this._realScale >= 2) this._realScale = 2;
        else if (this._realScale >= 1.5) this._realScale = 1.5;
        else if (this._realScale >= 1) this._realScale = 1;
        else this._realScale = 0.5;
      } else {
        this._realScale = this._scale;
      }
    };
    
    }; // Yanfly.Param.UpdateRealScale
    
    Graphics.printFullError = function(name, message, stack) {
      stack = this.processErrorStackMessage(stack);
      if (this._errorPrinter) {
        this._errorPrinter.innerHTML =
          this._makeFullErrorHtml(name, message, stack);
      }
      this._applyCanvasFilter();
      this._clearUpperCanvas();
    };
    
    Graphics._makeFullErrorHtml = function(name, message, stack) {
      var text = '';
      for (var i = 2; i < stack.length; ++i) {
        text += '<font color=white>' + stack[i] + '</font><br>';
      }
      return ('<font color="yellow"><b>' + stack[0] + '</b></font><br>' +
        '<font color="yellow"><b>' + stack[1] + '</b></font><br>' + text);
    };
    
    Graphics.processErrorStackMessage = function(stack)  {
      var data = stack.split(/(?:\r\n|\r|\n)/);
      data.unshift('Game has encountered a bug. Please report it.<br>');
      for (var i = 1; i < data.length; ++i) {
        data[i] = data[i].replace(/[\(](.*[\/])/, '(');
      }
      data.push('<br><font color="yellow"><b>Press F5 to restart the game.' +
        '</b></font><br>');
      return data;
    };
    
    Yanfly.Core.Graphics_updateErrorPrinter = Graphics._updateErrorPrinter;
    Graphics._updateErrorPrinter = function() {
      Yanfly.Core.Graphics_updateErrorPrinter.call(this);
      this._errorPrinter.height = this._height * 0.5;
      this._errorPrinter.style.textAlign = 'left';
      this._centerElement(this._errorPrinter);
    };
    
    SceneManager.catchException = function(e) {
      if (e instanceof Error) {
        Graphics.printFullError(e.name, e.message, e.stack);
        console.error(e.stack);
      } else {
        Graphics.printError('UnknownError', e);
      }
      AudioManager.stopAll();
      this.stop();
    };
    